import { createRouter, createWebHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'
import Layout from '@/layout/index.vue'

const routes: RouteRecordRaw[] = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/login/index.vue'),
    meta: { title: '登录', hideInMenu: true }
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    meta: { hideInMenu: true },
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/dashboard/index.vue'),
        meta: { 
          title: '控制台', 
          icon: 'Monitor',
          affix: true 
        }
      }
    ]
  },
  {
    path: '/dashboard',
    component: Layout,
    meta: { title: '控制台', icon: 'Monitor' },
    children: [
      {
        path: '',
        name: 'DashboardIndex',
        component: () => import('@/views/dashboard/index.vue'),
        meta: { 
          title: '控制台', 
          icon: 'Monitor',
          affix: true 
        }
      }
    ]
  },
  {
    path: '/trading',
    component: Layout,
    redirect: '/trading/strategy',
    meta: { title: '交易管理', icon: 'TrendCharts' },
    children: [
      {
        path: 'strategy',
        name: 'Strategy',
        component: () => import('@/views/trading/strategy/index.vue'),
        meta: { title: '策略管理', icon: 'SetUp' }
      },
      {
        path: 'strategy/:id',
        name: 'StrategyDetail',
        component: () => import('@/views/trading/strategy/detail.vue'),
        meta: { title: '策略详情', icon: 'SetUp', hideInMenu: true }
      },
      {
        path: 'positions',
        name: 'Positions',
        component: () => import('@/views/trading/positions/index.vue'),
        meta: { title: '持仓管理', icon: 'Wallet' }
      },
      {
        path: 'orders',
        name: 'Orders',
        component: () => import('@/views/trading/orders/index.vue'),
        meta: { title: '订单历史', icon: 'Document' }
      }
    ]
  },
  {
    path: '/market',
    component: Layout,
    redirect: '/market/overview',
    meta: { title: '市场分析', icon: 'DataAnalysis' },
    children: [
      {
        path: 'overview',
        name: 'MarketOverview',
        component: () => import('@/views/market/overview/index.vue'),
        meta: { title: '市场概览', icon: 'PieChart' }
      },
      {
        path: 'technical',
        name: 'TechnicalAnalysis',
        component: () => import('@/views/market/technical/index.vue'),
        meta: { title: '技术分析', icon: 'DataLine' }
      },
      {
        path: 'signals',
        name: 'TradingSignals',
        component: () => import('@/views/market/signals/index.vue'),
        meta: { title: '交易信号', icon: 'Bell' }
      },
      {
        path: 'arbitrage',
        name: 'ArbitrageAnalysis',
        component: () => import('@/views/market/arbitrage/index.vue'),
        meta: { title: '套利分析', icon: 'Switch' }
      }
    ]
  },
  {
    path: '/risk',
    component: Layout,
    redirect: '/risk/monitor',
    meta: { title: '风险管理', icon: 'Shield' },
    children: [
      {
        path: 'monitor',
        name: 'RiskMonitor',
        component: () => import('@/views/risk/monitor/index.vue'),
        meta: { title: '风险监控', icon: 'Monitor' }
      },
      {
        path: 'settings',
        name: 'RiskSettings',
        component: () => import('@/views/risk/settings/index.vue'),
        meta: { title: '风险设置', icon: 'Setting' }
      }
    ]
  },
  {
    path: '/exchanges',
    component: Layout,
    redirect: '/exchanges/connections',
    meta: { title: '交易所管理', icon: 'Connection' },
    children: [
      {
        path: 'connections',
        name: 'ExchangeConnections',
        component: () => import('@/views/exchanges/connections/index.vue'),
        meta: { title: '连接配置', icon: 'Link' }
      },
      {
        path: 'api-management',
        name: 'ApiManagement',
        component: () => import('@/views/exchanges/api-management/index.vue'),
        meta: { title: 'API管理', icon: 'Key' }
      },
      {
        path: 'status-monitor',
        name: 'StatusMonitor',
        component: () => import('@/views/exchanges/status/index.vue'),
        meta: { title: '连接状态', icon: 'Monitor' }
      },
      {
        path: 'rate-limits',
        name: 'RateLimits',
        component: () => import('@/views/exchanges/rate-limits/index.vue'),
        meta: { title: '频率限制', icon: 'Timer' }
      }
    ]
  },
  {
    path: '/account',
    component: Layout,
    redirect: '/account/assets',
    meta: { title: '账户管理', icon: 'User' },
    children: [
      {
        path: 'assets',
        name: 'Assets',
        component: () => import('@/views/account/assets/index.vue'),
        meta: { title: '资产管理', icon: 'Money' }
      },
      {
        path: 'profile',
        name: 'Profile',
        component: () => import('@/views/account/profile/index.vue'),
        meta: { title: '个人设置', icon: 'Setting' }
      }
    ]
  },
  {
    path: '/terminal',
    component: Layout,
    redirect: '/terminal/trading',
    meta: { title: '交易终端', icon: 'Monitor' },
    children: [
      {
        path: 'trading',
        name: 'TradingTerminal',
        component: () => import('@/views/terminal/trading/index.vue'),
        meta: { title: '专业交易', icon: 'TrendCharts' }
      }
      // 一期暂时隐藏K线分析和深度分析，专注于策略功能
      // {
      //   path: 'charts',
      //   name: 'Charts',
      //   component: () => import('@/views/terminal/charts/index.vue'),
      //   meta: { title: 'K线分析', icon: 'DataLine' }
      // },
      // {
      //   path: 'orderbook',
      //   name: 'OrderBook',
      //   component: () => import('@/views/terminal/orderbook/index.vue'),
      //   meta: { title: '深度分析', icon: 'DataBoard' }
      // }
    ]
  },
  {
    path: '/analytics',
    component: Layout,
    redirect: '/analytics/market',
    meta: { title: '数据分析', icon: 'DataAnalysis' },
    children: [
      {
        path: 'market',
        name: 'MarketAnalytics',
        component: () => import('@/views/analytics/market/index.vue'),
        meta: { title: '市场分析', icon: 'TrendCharts' }
      },
      {
        path: 'strategy',
        name: 'StrategyAnalytics',
        component: () => import('@/views/analytics/strategy/index.vue'),
        meta: { title: '策略分析', icon: 'DataBoard' }
      },
      {
        path: 'risk-analytics',
        name: 'RiskAnalytics',
        component: () => import('@/views/analytics/risk/index.vue'),
        meta: { title: '风险分析', icon: 'Warning' }
      }
    ]
  },
  // 一期暂时隐藏社区交流功能，专注于策略功能
  // {
  //   path: '/community',
  //   component: Layout,
  //   redirect: '/community/strategies',
  //   meta: { title: '社区交流', icon: 'ChatDotRound' },
  //   children: [
  //     {
  //       path: 'strategies',
  //       name: 'StrategyShare',
  //       component: () => import('@/views/community/strategies/index.vue'),
  //       meta: { title: '策略分享', icon: 'Share' }
  //     },
  //     {
  //       path: 'discussions',
  //       name: 'Discussions',
  //       component: () => import('@/views/community/discussions/index.vue'),
  //       meta: { title: '行情讨论', icon: 'ChatLineRound' }
  //     },
  //     {
  //       path: 'experts',
  //       name: 'ExpertQA',
  //       component: () => import('@/views/community/experts/index.vue'),
  //       meta: { title: '专家问答', icon: 'User' }
  //     }
  //   ]
  // },
  // 一期暂时隐藏AI助手功能，专注于策略功能
  // {
  //   path: '/ai-assistant',
  //   component: Layout,
  //   redirect: '/ai-assistant/recommendations',
  //   meta: { title: 'AI助手', icon: 'MagicStick' },
  //   children: [
  //     {
  //       path: 'recommendations',
  //       name: 'AIRecommendations',
  //       component: () => import('@/views/ai/recommendations/index.vue'),
  //       meta: { title: '智能推荐', icon: 'Star' }
  //     },
  //     {
  //       path: 'chat',
  //       name: 'AIChat',
  //       component: () => import('@/views/ai/chat/index.vue'),
  //       meta: { title: '智能问答', icon: 'ChatDotRound' }
  //     },
  //     {
  //       path: 'optimization',
  //       name: 'AIOptimization',
  //       component: () => import('@/views/ai/optimization/index.vue'),
  //       meta: { title: '参数优化', icon: 'Setting' }
  //     }
  //   ]
  // },
  {
    path: '/system',
    component: Layout,
    redirect: '/system/logs',
    meta: { title: '系统管理', icon: 'Tools' },
    children: [
      {
        path: 'logs',
        name: 'SystemLogs',
        component: () => import('@/views/system/logs/index.vue'),
        meta: { title: '系统日志', icon: 'Document' }
      },
      {
        path: 'notifications',
        name: 'Notifications',
        component: () => import('@/views/system/notifications/index.vue'),
        meta: { title: '通知设置', icon: 'Bell' }
      },
      {
        path: 'backup',
        name: 'Backup',
        component: () => import('@/views/system/backup/index.vue'),
        meta: { title: '数据备份', icon: 'FolderOpened' }
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return { top: 0 }
    }
  }
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 设置页面标题
  document.title = to.meta?.title 
    ? `${to.meta.title} - Web3量化交易系统` 
    : 'Web3量化交易系统'
  
  // 这里可以添加登录验证逻辑
  // const token = localStorage.getItem('token')
  // if (!token && to.path !== '/login') {
  //   next('/login')
  //   return
  // }
  
  next()
})

export default router 